//
//  SHPTabBar.swift
//  weibo
//
//  Created by songhuipan on 16/4/16.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit

// 声明一个协议
protocol SHPTabBarDelegate: NSObjectProtocol {
    
    // 协议方法
    func composeButtonClick()
}

class SHPTabBar: UITabBar {
    // 声明一个代理
    weak var SHPDelegate:SHPTabBarDelegate?
    
    override init(frame: CGRect) {
        
        super.init(frame:frame)
        
        addSubview(composeButton)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 点击加号按钮时调用的函数
    @objc private func composeButtonClick() {
        
        // 代理变量调用代理方法
        SHPDelegate?.composeButtonClick()
        
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        
        let width = UIScreen.mainScreen().bounds.size.width / 5
        
        var index:CGFloat = 0
                
        for value in self.subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame.origin.x = width * index
                value.frame.size.width = width
                
                index++
                
                if index == 2{
                    // 这里++符号必须紧挨index，否则报错
                    index++
                }
            }
        }
        
        composeButton.center.x = self.frame.width*0.5
        composeButton.center.y = self.frame.height*0.5
        
    }
    // MRAK: - 懒加载按钮
    private lazy var composeButton :UIButton = {
        
        let button = UIButton(imageName: "tabbar_compose_icon_add", backIamgeName: "tabbar_compose_button")
        
        button .addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)

        return button
    }()
    
}

















